new Vue({
	el: '#app',
	data: {
		uid: '',
		sid: '',
		id: '',
		tipList: [],
		scoreList: [],
		page: '1',
		t: [],
		path: '',
		select_id: '',
		content: '',
		point: '',
		tag_map: [],
		tip_map: []
	},
	mounted: function() {
		console.log("mounted end");
		this.uid = $api.getStorage('uid');
		this.init();
	},
	methods: {
		init: function() {
			var self = this;
			self.tips();
//			self.score();
			self.selectId(1);
			apiready = function() {
				var fid = api.pageParam;
				self.sid = fid.id;
				//loading加载
				self.isS();
				//滚动到底部加载更多
//				api.addEventListener({
//					name: 'scrolltobottom',
//					extra: {
//						threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
//					}
//				}, function(ret, err) {
//					//alert('已滚动到底部');
//					self.page++;
//					if(self.page > self.pageCount) {
//						api.toast({
//							msg: '没有更多了',
//							duration: 1000,
//							location: 'bottom'
//						});
//					} else {
//						self.score();
//					}
//				});
			}
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				self.score();
			}
		},
		//评分选择
		selectId: function(s_id) {
			this.select_id = s_id;
		},
		//获取评分标签列表
		tips: function() {
			var self = this;
			var data = {};
			$.get(base + config.study_taglist, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.tipList = msg.data;
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//
		choose: function(tip_id) {
			console.log(tip_id);
			var ischoose = $("[name = tip]#" + tip_id + "").hasClass("active");
			console.log(ischoose);
			if(ischoose) {
				$("[name = tip]#" + tip_id + "").removeClass("active");
				for (var i = 0; i<this.tip_map.length; i++) {
					if (tip_id == this.tip_map[i]) {
						this.tip_map.splice(i,1);
					}
				}
			} else {
				$("[name = tip]#" + tip_id + "").addClass("active");
				this.tip_map.push(tip_id);
			}
			console.log(this.tip_map);
		},
		//评论列表
		score: function() {
			var self = this;
			var data = {
				study_lesson_id: self.sid
			};
			$.post(base + config.study_comment_list, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.t = msg.data;
					for(var i = 0; i < self.t.length; i++) {
						self.scoreList.push(self.t[i]);
					};
					self.pageCount = msg.page_count;
					self.path = msg.url;
					self.isShow = true;
					self.isS();
				} else {
					self.isShow = true;
					self.isS();
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//显示点击的评论
		showThis: function(id) {
			var self = this;
			self.id = id;
			var data = {
				id: id,
				uid: self.uid
			};
			$.post(base + config.study_comment_edit, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.content = msg.comment.content;
					self.selectId(msg.point.point);
					self.tag_map = msg.tag_map;
					$("[name = tip]").removeClass("active");
					for(var i = 0; i < msg.tag_map.length; i++) {
						self.choose(msg.tag_map[i]);
					}
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//提交评论
		submitScore: function() {
			var self = this;
			var data = {
				content: self.content,
				id: self.id,
				point: self.select_id,
				uid: self.uid,
				study_lesson_id: self.sid,
				tag: self.tip_map
			};
			$.post(base + config.study_comment_save, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.id = "";
					$("[name = tip]").removeClass("active");
					self.selectId(1);
					self.content = "";
					self.scoreList = [];
					api.toast({
						msg: "发布成功",
						duration: 2000,
						location: 'middle'
					});
					self.score();
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		}
	}

})